/** 
 * @CopyRight (c) 2012-2012
 * @Project <eshop>
 * @JDK_Version_Used:<JDK1.6>
 * @Description:<统一分页查询web>
 * @Title: SearchWeb.java
 * @Author: <何俊>
 * @Author_of_Contact <E:azj1001@sina.com T:18651831939>
 * @Date 2012-11-13 下午3:34:58
 * @Modified_By: <修改人>
 * @Modified_of_Contact <修改人联系方式>
 * @Modified_Date <修改日期时间>
 * @Why & What is modified <修改原因描述>
 * @Version v1.00
 */
package com.e.buy.web.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.commons.query.ConditionBean;

import com.e.buy.BuyTools;
import com.e.buy.bean.QueryTree;
import com.e.buy.bean.back.manager.CmiAdminUser;
import com.e.buy.bean.back.manager.cmi_ad.CmiAd;
import com.e.buy.bean.back.manager.cmi_ad.CmiAdLocation;
import com.e.buy.bean.back.manager.cmi_ad.CmiAdType;
import com.e.buy.bean.back.manager.cmi_goods.CmiGoods;
import com.e.buy.bean.back.manager.cmi_goods.CmiGoodsBrand;
import com.e.buy.bean.back.manager.cmi_goods.CmiGoodsCategroy;
import com.e.buy.bean.back.manager.cmi_goods.CmiGoodsProperties;
import com.e.buy.bean.back.manager.cmi_news.CmiNews;
import com.e.buy.bean.back.manager.cmi_news.CmiNewsType;
import com.e.buy.bean.back.manager.cmi_power.CmiAdminLog;
import com.e.buy.bean.back.manager.cmi_power.CmiRole;
import com.e.buy.bean.back.manager.cmi_sigle.CmiSingle;
import com.e.buy.bean.back.manager.cmi_sigle.CmiSingleType;
import com.e.buy.bean.cmi_order.CmiOrder;
import com.e.buy.enums.Commons;
import com.e.buy.enums.Status;
import com.e.buy.service.CommonsServiceClient;

/**
 * @Description:(统一分页查询web)
 * @author <何俊>
 * @date 2012-11-13 下午3:34:58
 * @version v1.0
 * @Other :@see #Files
 */
@Controller
@RequestMapping("/impl_search")
public class AdminSearchWeb extends BuyTools {

	@Resource
	private CommonsServiceClient commons = null;

	public void setCommons(CommonsServiceClient commons) {
		this.commons = commons;
	}

	/**
	 * @Description:<统一查询>
	 * @title: paginSearch
	 * @param pageSize : 每页记录数
	 * @param pageNumber：当前页数
	 * @param qr : 查询条件
	 * @param flg : 操作标识符
	 * @param sortcol : 排序字段
	 * @param session
	 * @return String 返回类型
	 * @throws
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/commonsPaginSearch.ajax")
	public String paginSearch(@RequestParam("rows") int pageSize, @RequestParam("page") int pageNumber,
			@RequestParam("qr") String qr, @RequestParam("st") String st, @RequestParam("sco") String sortcol,
			@RequestParam("dc") int dc, HttpSession session) {

		JSONObject query = new JSONObject();
		Object total = "0";
		Commons.BackProcess e = Commons.getBackProcess(st);

		Class<?> c = null;
		List<?> ls = new ArrayList<Object>();
		Object[] args = new Object[0];
		boolean sortable = true;
		boolean desc = (dc == 1);
		List<ConditionBean> beans = new ArrayList<ConditionBean>();
		try {

			// 参数
			switch (e) {
			case ADMIN_POWER_CMI_ROLE:
				c = CmiRole.class;
				break;
			case ADMIN_INFO:
				c = CmiAdminUser.class;
				break;
			case ADMIN_LOG:
				c = CmiAdminLog.class;
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				break;
			case GOODS_CATEGROY:
				c = CmiGoodsCategroy.class;
				break;
			case GOODS_CATEGROY_PROPERTIES:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiGoodsProperties.class;
				break;
			case GOODS_BRAND:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiGoodsBrand.class;
				break;
			case GOODS:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiGoods.class;
				break;
			case NEWS:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiNews.class;
				break;
			case NEWS_TYPE:
				c = CmiNewsType.class;
				break;
			case SINGLE:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiSingle.class;
				break;
			case SINGLE_TYPE:
				c = CmiSingleType.class;
				break;
			case AD:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiAd.class;
				break;
			case AD_TYPE:
				c = CmiAdType.class;
				break;
			case AD_LOC:
				c = CmiAdLocation.class;
				break;
			case ORDER:
				beans = (List<ConditionBean>) toolsbox.jsonArraystr2List(qr, ConditionBean.class);
				c = CmiOrder.class;
			default:
				break;
			}
			// 分页
			total = this.commons.queryForObject(e, Status.TOTAL, String.class, beans, args);
			ls = this.commons.queryPagin(pageSize, pageNumber, c, e, sortable, sortcol, desc, beans, args);
			// 结果
			query.element("total", total);
			query.element("rows", toolsbox.list2JsonArrayStr(ls));
		} catch (Exception ex) {
			ex.printStackTrace();
			String eMsg = "后台管理[统一分页查询] 状态[" + st + "] 条件[" + qr + "] 页数[" + pageSize + "," + pageNumber + "] 查询失败:";
			log4j.error(eMsg + ex);
			query.element("total", "0");
			query.element("rows", "[]");
		}
		return query.toString();
	}

	@ResponseBody
	@RequestMapping("/searchObjectList.ajax")
	public String searchObjectList(@RequestParam("st") String st, @RequestParam("qy") String qy) {
		String res = null;
		List<?> ls = commons.queryObjectList(st, qy);
		if (ls != null) {
			res = toolsbox.list2JsonArrayStr(ls);
		} else {
			res = "[]";
		}
		return res;
	}

	/**
	 * @Description:<统一查询树>
	 * @title: queryForTree
	 * @param qr ：条件
	 * @param st ：查询标志
	 * @param pt ：额外的查询参数com.e.buy.bean.QyeryTree
	 * @return String 返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("/searchTreeList.ajax")
	public String queryForTree(@RequestParam("qr") String qr, @RequestParam("st") String st,
			@RequestParam("pt") String pt) {
		JSONObject ret = new JSONObject();
		int errorCode = 0;
		String datas = "";
		Status state = Status.TREE;
		Object[] args = new Object[0];
		String msg = this.getWebProperties("web", "msg.success");
		Commons.BackProcess e = Commons.BackProcess.DEFAULT;
		List<ConditionBean> beans = new ArrayList<ConditionBean>();

		try {
			e = Commons.getBackProcess(st);
			switch (e) {
			case ADMIN_INFO:
				QueryTree qrt = (QueryTree) toolsbox.jsonstr2Object(pt, QueryTree.class);
				//所有权限
				List<?> system = commons.queryTree(Commons.BackProcess.ADMIN_SYSTEM, Status.TREE, beans, args);
				ret.element("system", toolsbox.list2JsonArrayStr(system));
				
				//自身权限
				args = new Object[] {qrt.getUserId()};
				break;
			default:
				msg = this.getWebProperties("web", "msg.fail.undefiend.status");
				errorCode = -1;
				break;
			}
			List<?> trees = commons.queryTree(e, state, beans, args);
			datas = toolsbox.list2JsonArrayStr(trees);
		} catch (Exception ex) {
			errorCode = 1;
			msg = this.getWebProperties("web", "msg.fail");
		}
		ret.element("code", errorCode);
		ret.element("msg", msg);
		ret.element("datas", datas);
		return ret.toString();
	}
}
